package com.android.project.sharingdata;

import java.util.Calendar;
import com.android.project.sharingdata.SharedInformation.Sharing;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        insertRecords();
		displayContentProvider();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void displayContentProvider() {
    	Log.i("test display", "je suis dans le display");
    	
    	String columns[] = new String[] { 
    			SharedInformation.Sharing.SHARING_ID,
    			SharedInformation.Sharing.CLIENT,
    			SharedInformation.Sharing.DATE
    	};
    	
    	String strUri = ""+Sharing.CONTENT_URI+"/sharing/5";
    	Log.i("test display uri", "URI : "+Sharing.CONTENT_URI + strUri);
    	Uri mShare = Uri.parse(strUri);
    	
		Cursor cur = managedQuery(mShare, columns, null, null, null);
		Toast.makeText(MainActivity.this, cur.getCount() + "", Toast.LENGTH_LONG).show();

		if (cur.moveToFirst()) {
			String name = null;
			do {
				name = cur.getString(cur.getColumnIndex(SharedInformation.Sharing.SHARING_ID)) + " " +
					   cur.getString(cur.getColumnIndex(SharedInformation.Sharing.CLIENT)) + " " + 
					   cur.getString(cur.getColumnIndex(SharedInformation.Sharing.DATE));
				Toast.makeText(this, name + " ", Toast.LENGTH_LONG).show();
			} while (cur.moveToNext());
		}
		
	}

	private void insertRecords() {
		ContentValues contact = new ContentValues();
		contact.put(Sharing.SHARE, "un partage");
		contact.put(Sharing.CLIENT, "Introduction à la programmation sous Android");
		contact.put(Sharing.DATE, Calendar.getInstance().getTimeInMillis());
		getContentResolver().insert(Sharing.CONTENT_URI, contact);

		contact.clear();
		contact.put(Sharing.SHARE, "un partage");
		contact.put(SharedInformation.Sharing.CLIENT, "Introduction à la programmation Java");
		contact.put(SharedInformation.Sharing.DATE, Calendar.getInstance().getTimeInMillis());
		getContentResolver().insert(Sharing.CONTENT_URI, contact);

		contact.clear();
		contact.put(Sharing.SHARE, "un partage");
		contact.put(SharedInformation.Sharing.CLIENT, "Introduction à l'objectif C");
		contact.put(SharedInformation.Sharing.DATE, Calendar.getInstance().getTimeInMillis());
		getContentResolver().insert(Sharing.CONTENT_URI, contact);
		
	}
}
